package com.github.binarywang.demo.wechat.service.impl;

import com.github.binarywang.demo.wechat.dao.CommentMapper;
import com.github.binarywang.demo.wechat.dao.CourseMapper;
import com.github.binarywang.demo.wechat.dao.UserMapper;
import com.github.binarywang.demo.wechat.model.Comment;
import com.github.binarywang.demo.wechat.model.CommentKey;
import com.github.binarywang.demo.wechat.model.Course;
import com.github.binarywang.demo.wechat.model.User;
import com.github.binarywang.demo.wechat.model.vo.CommentVO;
import com.github.binarywang.demo.wechat.service.WxCommentService;
import com.github.binarywang.demo.wechat.utils.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("wxCommentService")
public class WxCommentServiceImpl implements WxCommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CourseMapper courseMapper;

    public int insert(Comment comment) {
        if ((comment.getContent() != null) && (comment.getContent().startsWith(","))) {
            comment.setContent(comment.getContent().substring(1, comment.getContent().length()));
        }
        comment.setCreatetime(new Date());
        return this.commentMapper.insert(comment);
    }

    public List<CommentVO> list(Integer start) {
        List<CommentVO> resultList = new ArrayList();
        List<Comment> sourceList = this.commentMapper.selectAll();
        for (Comment comment : sourceList) {
            CommentVO commentVO = new CommentVO();
            BeanUtils.copyProperties(comment, commentVO);
            User user = this.userMapper.selectByPrimaryKey(comment.getOpenId());
            commentVO.setUser(user);
            Course course = this.courseMapper.selectByPrimaryKey(comment.getCourseId());
            commentVO.setCourse(course);
            resultList.add(commentVO);
        }
        resultList = Page.getCurrentPage(resultList, start);
        return resultList;
    }

    public List<CommentVO> findByCourseId(Integer courseId, Integer start) {
        List<CommentVO> resultList = new ArrayList();
        List<Comment> sourceList = this.commentMapper.selectByCourseId(courseId);
        for (Comment comment : sourceList) {
            CommentVO commentVO = new CommentVO();
            BeanUtils.copyProperties(comment, commentVO);
            User user = this.userMapper.selectByPrimaryKey(comment.getOpenId());
            commentVO.setUser(user);
            Course course = this.courseMapper.selectByPrimaryKey(comment.getCourseId());
            commentVO.setCourse(course);
            resultList.add(commentVO);
        }
        resultList = Page.getCurrentPage(resultList, start);
        return resultList;
    }

    public List<CommentVO> findByOpenId(String openId, Integer start) {
        List<CommentVO> resultList = new ArrayList();
        List<Comment> sourceList = this.commentMapper.selectByOpenId(openId);
        for (Comment comment : sourceList) {
            CommentVO commentVO = new CommentVO();
            BeanUtils.copyProperties(comment, commentVO);
            User user = this.userMapper.selectByPrimaryKey(comment.getOpenId());
            commentVO.setUser(user);
            Course course = this.courseMapper.selectByPrimaryKey(comment.getCourseId());
            commentVO.setCourse(course);
            resultList.add(commentVO);
        }
        resultList = Page.getCurrentPage(resultList, start);
        return resultList;
    }

    public int deleteByPrimaryKey(CommentKey key) {
        return this.commentMapper.deleteByPrimaryKey(key);
    }

    public int deleteByCourseId(Integer courseId) {
        return this.commentMapper.deleteByCourseId(courseId);
    }
}

